var imgArray, lbimgArray, app = getApp(),
    util = require("../../../utils/util.js"),
    siteinfo = require("../../../../siteinfo.js"),
    imgArray1 = [],
    lbimgArray1 = [],
    imglogo = "";

Page({
    data: {
        mdid: "",
        issq: !1,
        isbj: !0,
        isyz: !0,
        isbd: !1,
        url1: "",
        url2: "",
        VerifyCode: "验证",
        bdsjhtext: "验证微信手机号",
        lxr: "",
        sjh: "",
        yzm: "",
        jwd: "",
        mdmc: "",
        mdgg: "",
        zsnum: 0,
        hy: [],
        checkbox: [],
        hyIndex: 0,
        timestart: "06:00",
        timeend: "22:00",
        weizhi: "",
        sfyy: 2,
        checkboxItems: [{
            name: "WIFI",
            value: "WIFI"
        }, {
            name: "停车位",
            value: "停车位"
        }, {
            name: "支付宝支付",
            value: "支付宝支付"
        }, {
            name: "微信支付",
            value: "微信支付"
        }],
        logo: "../../img/logo.png",
        images: [],
        images1: [],
        lbimages: [],
        lbimages1: [],
        uploadedImages: [],
        fwxy: !0,
        timess: '请设置营业时间',
        hidden_status: 1,
        listpath: [],
        SaveListPath: [],

    },
    lookck: function() {
        this.setData({
            fwxy: !1
        });
    },
    queren: function() {
        this.setData({
            fwxy: !0
        });
    },
    hqsjh: function(e) {
        this.setData({
                sjh: e.detail.value
            }),
            "" != e.detail.value ?
            this.setData({
                isyz: !1
            }) :
            this.setData({
                isyz: !0
            });
    },
    setVerify: function() {
        var e = util.getRandomNum();
        this.setData({
            yzm: e
        });
        var a = this.data.sjh;
        var t = 60,
            o = this;
        if (!/^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/.test(a) || 11 != a.length) return wx.showToast({
            title: "手机号错误",
            duration: 1e3
        }), !1;
        var i = setInterval(function() {
            0 < --t ? o.setData({
                VerifyCode: t + " 秒",
                isyz: !0
            }) : (o.setData({
                VerifyCode: "验证",
                isyz: !1
            }), clearInterval(i));
        }, 1e3);
        app.util.request({
            url: "entry/wxapp/sms2",
            cachetime: "0",
            headers: {
                "Content-Type": "application/json"
            },
            data: {
                tel: a,
                code: e
            },
            success: function(e) {
                "操作成功" == e.data.reason && wx.showToast({
                    title: "发送成功",
                    icon: "success",
                    duration: 1e3
                });
            },
            fail: function(e) {}
        });
    },
    dw: function() {
        var a = this;
        wx.chooseLocation({
            success: function(e) {
                a.setData({
                    weizhi: e.address,
                    jwd: e.latitude + "," + e.longitude
                });
            }
        });
    },
    bindTimeChange: function(e) {
        this.setData({
            timestart: e.detail.value
        });
    },
    bindTimeChange1: function(e) {
        this.setData({
            timeend: e.detail.value
        });
    },
    bindTypeChange: function(e) {
        this.setData({
            hyIndex: e.detail.value,
            hyid: this.data.hy[e.detail.value].id
        });
    },
    checkboxChange: function(e) {
        this.setData({
            checkbox: e.detail.value
        });
        for (var a = this.data.checkboxItems, t = e.detail.value, o = 0, i = a.length; o < i; ++o) {
            a[o].checked = !1;
            for (var l = 0, s = t.length; l < s; ++l)
                if (a[o].value == t[l]) {
                    a[o].checked = !0;
                    break;
                }
        }
        this.setData({
            checkboxItems: a
        });
    },
    chooseLogo: function() {
        var t = this;
        wx.chooseImage({
            count: 1,
            sourceType: ["album", "camera"],
            success: function(e) {
                wx.showToast({
                    icon: "loading",
                    title: "正在上传"
                });
                var a = e.tempFilePaths;
                wx.uploadFile({
                    url: siteinfo.siteroot + "?i=" + siteinfo.uniacid + "&c=entry&a=wxapp&do=upload&m=zh_cjdianc",
                    filePath: e.tempFilePaths[0],
                    name: "upfile",
                    success: function(e) {
                        imglogo = e.data, "" != e.data ? t.setData({
                            logo: a[0]
                        }) : wx.showModal({
                            title: "提示",
                            content: "上传失败",
                            showCancel: !1
                        });
                    },
                    fail: function(e) {
                        wx.showModal({
                            title: "提示",
                            content: "上传失败",
                            showCancel: !1
                        });
                    },
                    complete: function() {
                        wx.hideToast();
                    }
                });
            }
        });
    },
    chooseImage1: function() {
        var o = this,
            i = this.data.images1;
        imgArray1 = [], wx.chooseImage({
            count: 9 - i.length - o.data.images.length,
            sourceType: ["album", "camera"],
            success: function(e) {
                var a = e.tempFilePaths;
                var t = e.tempFilePaths;
                i = i.concat(t),
                    o.setData({
                        images1: i
                    });
            }
        });
    },
    lbchooseImage1: function() {
        var o = this,
            i = this.data.lbimages1;
        lbimgArray1 = [], wx.chooseImage({
            count: 3 - i.length - o.data.lbimages.length,
            sizeType: ["compressed"],
            sourceType: ["album", "camera"],
            success: function(e) {
                wx.showToast({
                    icon: "loading",
                    title: "正在上传"
                });
                var a = e.tempFilePaths;
                var t = e.tempFilePaths;
                i = i.concat(t),
                    o.lbuploadimg1({
                        url: getApp().imglink + "app/index.php?i=" + getApp().getuniacid + "&c=entry&a=wxapp&do=upload&m=zh_zbkq",
                        path: i
                    });
            }
        });
    },
    previewImage: function() {
        wx.previewImage({
            urls: this.data.images
        });
    },
    uploadimg1: function(e) {
        var a = this,
            t = e.i ? e.i : 0,
            o = e.success ? e.success : 0,
            i = e.fail ? e.fail : 0;
        wx.uploadFile({
            url: e.url,
            filePath: e.path[t],
            name: "upfile",
            formData: null,
            success: function(e) {
                "" != e.data ? (o++, imgArray1.push(e.data)) : wx.showToast({
                    icon: "loading",
                    title: "请重试"
                });
            },
            fail: function(e) {
                i++;
            },
            complete: function() {
                ++t == e.path.length ? (a.setData({
                    images1: e.path
                }), wx.hideToast()) : (e.i = t, e.success = o, e.fail = i, a.uploadimg1(e));
            }
        });
    },
    lbuploadimg1: function(e) {
        var a = this,
            t = e.i ? e.i : 0,
            o = e.success ? e.success : 0,
            i = e.fail ? e.fail : 0;
        wx.uploadFile({
            url: e.url,
            filePath: e.path[t],
            name: "upfile",
            formData: null,
            success: function(e) {
                "" != e.data ?
                    (o++, lbimgArray1.push(e.data)) :
                    wx.showToast({
                        icon: "loading",
                        title: "请重试"
                    });
            },
            fail: function(e) {
                i++;
            },
            complete: function() {
                ++t == e.path.length ? (
                    a.setData({
                        lbimages1: e.path
                    }), wx.hideToast()) : (
                    e.i = t, e.success = o, e.fail = i, a.lbuploadimg1(e));
            }
        });
    },
    delete: function(e) {
        var a = e.currentTarget.dataset.index,
            t = this.data.images;
        t.splice(a, 1), imgArray.splice(a, 1)
        this.setData({
            images: t
        });
    },
    delete1: function(e) {
        var a = e.currentTarget.dataset.index,
            t = this.data.images1;
        t.splice(a, 1),
            this.setData({
                images1: t
            });
    },
    lbdelete: function(e) {
        var a = e.currentTarget.dataset.index,
            t = this.data.lbimages;
        t.splice(a, 1), lbimgArray.splice(a, 1),

            this.setData({
                lbimages: t
            });
    },
    lbdelete1: function(e) {
        var a = e.currentTarget.dataset.index,
            t = this.data.lbimages1;
        t.splice(a, 1), lbimgArray1.splice(a, 1),
            this.setData({
                lbimages1: t
            });
    },
    formSubmit: function(e) {
        var a = this,
            t = a.data.images1;
        var o = wx.getStorageSync("sjdsjid");
        var i = this.data.yzm;
        var l = e.detail.value.sjmc,
            s = e.detail.value.sjdh,
            n = e.detail.value.mdwz,
            c = this.data.jwd,
            r = e.detail.value.rjj,
            g = e.detail.value.qsj,
            u = e.detail.value.xyh,
            d = e.detail.value.xyhje,
            m = e.detail.value.mdgg,
            jj = e.detail.value.jianjie,
            s_rest = e.detail.value.radiosfyy,
            psjg = e.detail.value.psjg || 0;

        var h = "",
            f = !0;
        if ("" == imglogo) h = "请上传商家Logo！";
        else if ("" == l) h = "请填写商家名称！";
        else if ("" == s) h = "请填写商家手机号！";
        else if (11 != s.length) h = "手机号错误！";
        else if ("" == n) h = "请填写门店位置";
        else if ("" == c) h = "请点击定位按钮进行定位";
        else if ("" == g) h = "请填写起送价";
        else if ("" == m) h = "请填写门店公告";
        // else if ("" == r) h = "请填写人均价";
        // else if ("" == d) h = "请填写新用户优惠金额";
        else {
            var p = function() {
                var e = imgArray.concat(imgArray1);
                app.util.request({
                    url: "entry/wxapp/UpdStoreInfo",
                    cachetime: "0",
                    data: {
                        id: o,
                        logo: imglogo,
                        name: l,
                        tel: s,
                        address: n,
                        coordinates: c,
                        capita: r,
                        start_at: g,
                        announcement: m,
                        details: jj,
                        is_rest: s_rest,
                        psjg: psjg,
                        xyh_money: d,
                        xyh_open: u ? 1 : 2,
                        environment: e.toString()
                    },
                    success: function(e) {
                        "1" == e.data ?
                            (wx.showModal({
                                title: "提示",
                                content: "提交成功"
                            }), setTimeout(function() {
                                wx.navigateBack({});
                            }, 1e3)) :
                            "2" == e.data ?
                            wx.showModal({
                                title: "提示",
                                content: "请修改后提交"
                            }) : wx.showToast({
                                title: "网络错误"
                            });
                    }
                });
            };
            f = !1, wx.showLoading({
                title: "正在提交",
                mask: !0
            }), 0 == t.length ? p() : function e(a) {
                var t = a.i ? a.i : 0,
                    o = a.success ? a.success : 0,
                    i = a.fail ? a.fail : 0;
                wx.uploadFile({
                    url: a.url,
                    filePath: a.path[t],
                    name: "upfile",
                    formData: null,
                    success: function(e) {
                        "" != e.data ? (o++, imgArray1.push(e.data)) : wx.showToast({
                            icon: "loading",
                            title: "请重试"
                        });
                    },
                    fail: function(e) {
                        i++;
                    },
                    complete: function() {
                        ++t == a.path.length ? (wx.hideToast(), p()) : (a.i = t, a.success = o,
                            a.fail = i, e(a));
                    }
                });
            }({
                url: siteinfo.siteroot + "?i=" + siteinfo.uniacid + "&c=entry&a=wxapp&do=upload&m=zh_cjdianc",
                path: t
            });
        }
        1 == f && wx.showModal({
            title: "提示",
            content: h
        });
    },
    cxkt: function() {
        this.setData({
            issq: !0
        });
    },
    gongg: function(e) {
        var a = parseInt(e.detail.value.length);
        this.setData({
            zsnum: a
        });
    },
    getPhoneNumber: function(e) {
        var a = this;
        "getPhoneNumber:fail user deny" == e.detail.errMsg ?
            wx.showModal({
                title: "提示",
                showCancel: !1,
                content: "您未授权获取您的手机号",
                success: function(e) {}
            }) :
            app.util.request({
                url: "entry/wxapp/Jiemi",
                cachetime: "0",
                data: {
                    sessionKey: getApp().getSK,
                    data: e.detail.encryptedData,
                    iv: e.detail.iv
                },
                success: function(e) {
                    null != e.data.phoneNumber && a.setData({
                        sjh: e.data.phoneNumber,
                        isbd: !0,
                        bdsjhtext: "验证成功"
                    });
                }
            });
    },
    onLoad: function(e) {
        app.setNavigationBarColor(this);
        imgArray = [], imgArray1 = [], lbimgArray = [], lbimgArray1 = [];
        var a = wx.getStorageSync("users").id,
            t = wx.getStorageSync("sjdsjid"),
            o = this;
        app.util.request({
            url: "entry/wxapp/Url",
            cachetime: "0",
            success: function(e) {
                o.setData({
                    url: e.data
                });
            }
        });
        wx.showLoading({
            title: '加载中'
        });
        app.util.request({
            url: "entry/wxapp/StoreInfo",
            cachetime: "0",
            data: {
                store_id: t
            },
            success: function(e) {
                var a = e.data.store;
                if (e.data.psf[0]) {
                    o.setData({
                        psjg: e.data.psf[0].money
                    })
                } else {
                    o.setData({
                        psjg: 0
                    })
                }
                imglogo = a.logo,
                    imgArray = a.environment,
                    lbimgArray = a.yyzz,
                    o.setData({
                        timess: e.data.store.time + "-" + e.data.store.time4,
                        logo: a.logo,
                        sjmc: a.name,
                        sjdh: a.tel,
                        weizhi: a.address,
                        jwd: a.coordinates,
                        rjj: a.capita,
                        qsj: a.start_at,
                        xyhje: e.data.storeset.xyh_money,
                        mdgg: a.announcement,
                        jianjie: a.details,
                        zsnum: parseInt(a.announcement.length),
                        images: a.environment,
                        lbimages: a.yyzz,
                        xyh_open: 1 == e.data.storeset.xyh_open,
                        sfyy: a.is_rest
                    });
                wx.hideLoading();
            },
            fail: function() {
                wx.hideLoading();
            }
        });
    },

    onShow: function() {
        var t = wx.getStorageSync("sjdsjid"),
            o = this;
        if (wx.getStorageSync("is__ShuaXin")) {
            app.util.request({
                url: "entry/wxapp/StoreInfo",
                cachetime: "0",
                data: {
                    store_id: t
                },
                success: function(t) {
                    o.setData({
                        timess: t.data.store.time + "-" + t.data.store.time4
                    });
                    wx.setStorageSync("is__ShuaXin", false);
                }
            });
        }
    },
    changeHiddenStatus: function(t) {
        let hs = t.currentTarget.dataset.hidden_status,
            o = this,
            store_id = wx.getStorageSync("sjdsjid");
        this.setData({
            hidden_status: hs
        });
        if (hs == 2 && !o.data.yyzz_img) {
            app.util.request({
                url: "entry/wxapp/GetStoreZz",
                cachetime: "0",
                data: {
                    store_id: store_id
                },
                success: function(t) {
                    o.setData({
                        yyzz_img: t.data.res.yyzz,
                        jyxkz_img: t.data.res.jyxkz,
                        frsfz_img: t.data.res.frsfz,
                    })
                }
            });
        }
        if (hs == 3 && o.data.listpath.length == 0) {
            app.util.request({
                url: "entry/wxapp/GetStoreTp",
                cachetime: "0",
                data: {
                    store_id: store_id
                },
                success: function(res) {
                    let environment = res.data.res.environment.split(','),
                        new_listpath = [];
                    if (environment && environment.length > 0) {
                        for (var i = 0; i < environment.length; i++) {
                            new_listpath.push(o.data.url + environment[i]);
                        }
                        o.setData({
                            SaveListPath: environment,
                            listpath: new_listpath
                        })
                    }
                }
            });
        }
    },
    choose: function(dt) {
        var t = this,
            types = dt.currentTarget.dataset.types;

        wx.chooseImage({
            count: 1,
            sizeType: ["original", "compressed"],
            sourceType: ["album", "camera"],
            success: function(a) {
                wx.showLoading({
                    title: '加载中'
                });
                var e = a.tempFilePaths[0];
                wx.uploadFile({
                    url: siteinfo.siteroot + "?i=" + siteinfo.uniacid + "&c=entry&a=wxapp&do=upload&m=zh_cjdianc",
                    name: "upfile",
                    filePath: e,
                    formData: {},
                    success: function(res) {
                        wx.hideLoading();
                        if (res.statusCode == 413) {
                            wx.showModal({
                                title: '提示',
                                content: '上传图片过大',
                                showCancel: false
                            })
                            return false;
                        }
                        if (types == 1) {
                            t.setData({
                                yyzz_img: res.data
                            });
                        } else if (types == 2) {
                            t.setData({
                                frsfz_img: res.data
                            });
                        } else if (types == 3) {
                            t.setData({
                                jyxkz_img: res.data
                            });
                        }
                    },
                    fail: function() {
                        wx.hideLoading();
                    },
                    complete: function() {
                        wx.hideLoading();
                    }
                });
            }
        });
    },
    uploadQuali: function() {
        wx.showLoading({
            title: '保存中'
        });
        var ths = this,
            store_id = wx.getStorageSync("sjdsjid");
        app.util.request({
            url: "entry/wxapp/UpdateStoreZz",
            cachetime: "0",
            data: {
                yyzz: ths.data.yyzz_img,
                frsfz: ths.data.frsfz_img,
                jyxkz: ths.data.jyxkz_img,
                store_id: store_id
            },
            success: function(t) {
                wx.hideLoading();
                wx.showToast({
                    title: '保存成功',
                    icon: 'success'
                });
                setTimeout(() => {
                    wx.hideToast();
                }, 2000)
            },
            fail: function() {
                wx.hideLoading();
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    //上传商家图片
    upBusinessImg: function() {
        let ths = this;
        wx.chooseImage({
            count: 9,
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showToast({
                    title: '正在上传...',
                    icon: 'loading',
                    mask: true,
                    duration: 10000
                })
                let tempFilePaths = res.tempFilePaths;
                for (let i = 0; i < tempFilePaths.length; i++) {
                    wx.uploadFile({
                        url: siteinfo.siteroot + "?i=" + siteinfo.uniacid + "&c=entry&a=wxapp&do=upload&m=zh_cjdianc",
                        filePath: tempFilePaths[i],
                        name: "upfile",
                        formData: {},
                        success: function(res) {
                            wx.hideToast();
                            if (res.statusCode == 413) {
                                wx.showModal({
                                    title: '提示',
                                    content: '上传图片过大',
                                    showCancel: false
                                })
                                return false;
                            }
                            let list_path = ths.data.listpath;
                            list_path.push(ths.data.url + res.data);
                            ths.setData({
                                listpath: list_path
                            });
                            ths.data.SaveListPath = ths.data.SaveListPath.concat(res.data);
                        },
                        fail: function(res) {
                            wx.hideToast();
                            wx.showModal({
                                title: '错误提示',
                                content: '上传图片失败',
                                showCancel: false
                            });
                        }
                    })
                }
            }
        })
    },
    delImage: function(e) {
        let eidx = e.target.dataset.idx,
            list_path = this.data.listpath,
            new_SaveListPath = this.data.SaveListPath;
        list_path.splice(eidx, 1);
        new_SaveListPath.splice(eidx, 1);
        this.setData({
            listpath: list_path,
            SaveListPath: new_SaveListPath
        });
    },
    // 图片预览
    imgYu: function(event) {
        var src = event.currentTarget.dataset.src; //获取data-src
        var imgList = event.currentTarget.dataset.list; //获取data-list
        wx.previewImage({
            current: src, // 当前显示图片的http链接
            urls: imgList // 需要预览的图片http链接列表
        })
    },
    saveBusinessImg: function() {
        wx.showLoading({
            title: '保存中'
        });
        var ths = this,
            store_id = wx.getStorageSync("sjdsjid");
        app.util.request({
            url: "entry/wxapp/UpdateStoreTp",
            cachetime: "0",
            data: {
                environment: ths.data.SaveListPath,
                store_id: store_id
            },
            success: function(t) {
                wx.hideLoading();
                wx.showToast({
                    title: '保存成功',
                    icon: 'success'
                });
                setTimeout(() => {
                    wx.hideToast();
                }, 2000)
            },
            fail: function() {
                wx.hideLoading();
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
});